package com.msh.starter.captcha.bean;

import com.msh.frame.interfaces.ICaptcha;
import com.msh.starter.captcha.util.CaptchaUtil;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;

public abstract class AbstractCaptcha implements ICaptcha {
  /**
   * 字体
   */
  protected Font font;
  /**
   * 验证码图片默认宽度
   */
  protected int width;
  /**
   * 验证码图片默认高度
   */
  protected int height;

  public AbstractCaptcha(Font font, int width, int height) {
    this.font = font;
    this.width = width;
    this.height = height;
  }

  /**
   * 绘制干扰线条
   * @param g         Graphics2D
   */
  public void drawLine(Graphics2D g){
    drawLine(CaptchaUtil.nextInt(3,5),g);
  }

  /**
   * 绘制干扰线条
   * @param size      线条数量
   * @param g         Graphics2D
   */
  public void drawLine(int size,Graphics2D g){
    size = Math.max(size,3);
    for(int i=0;i<size;i++){
      g.setColor(CaptchaUtil.color());
      int x = CaptchaUtil.nextInt(5);
      int y = CaptchaUtil.nextInt(height);
      int _width = CaptchaUtil.nextInt(width-5,width);
      int _height = CaptchaUtil.nextInt(height);
      g.drawLine(x,y,_width,_height);
    }
  }
  /**
   * 绘制圆圈
   * @param g     Graphics2D
   */
  public void drawOval(Graphics2D g){
    drawOval(CaptchaUtil.nextInt(3,8),g);
  }

  /**
   * 绘制圆圈
   * @param size      数量
   * @param g         Graphics2D
   */
  public void drawOval(int size ,Graphics2D g){
    size = Math.max(size,3);
    for(int i=0;i<size;i++){
      g.setColor(CaptchaUtil.color());
      g.drawOval(CaptchaUtil.nextInt(width-20),CaptchaUtil.nextInt(height-10),CaptchaUtil.nextInt(5,15),CaptchaUtil.nextInt(5,15));
    }
  }

  /**
   * 绘制贝塞尔曲线
   * @param g         Graphics2D
   */
  public void drawBezierLine(Graphics2D g){
    drawBezierLine(CaptchaUtil.nextInt(2,5),g);
  }

  /**
   * 绘制贝塞尔曲线
   * @param size      曲线数量
   * @param g         Graphics2D
   */
  public void drawBezierLine(int size,Graphics2D g){
    size = Math.max(size, 2);
    for(int i=0;i<size;i++){
      g.setColor(CaptchaUtil.color());
      int x1 = 5,y1 = CaptchaUtil.nextInt(5,height/2);
      int x2 = width - 5,y2 = CaptchaUtil.nextInt(height/2,height-5);
      int ctrlX = CaptchaUtil.nextInt(width/4,width/4*3),ctrlY = CaptchaUtil.nextInt(5,height-5);
      if(CaptchaUtil.nextInt(2) == 0){
        int ty = y1;
        y1 = y2;
        y2 = ty;
      }
      if(CaptchaUtil.nextInt(2) == 0){
        //绘制二阶贝塞尔曲线
        QuadCurve2D shape = new QuadCurve2D.Double();
        shape.setCurve(x1,y1,ctrlX,ctrlY,x2,y2);
        g.draw(shape);
      }else{
        //绘制三阶贝塞尔曲线
        int ctrlX1 = CaptchaUtil.nextInt(width/4,width/4*3),ctrlY1 = CaptchaUtil.nextInt(5,height-5);
        CubicCurve2D shape =
            new CubicCurve2D.Double(x1,y1,ctrlX,ctrlY,ctrlX1,ctrlY1,x2,y2);
        g.draw(shape);
      }
    }
  }


  public int getDrawStringY(Rectangle2D rectangle2D){
    return  (height>>1) - (int)rectangle2D.getCenterY() + CaptchaUtil.randomOffset((int)rectangle2D.getHeight()/3);
  }

  public int getDrawStringX(Rectangle2D rectangle2D, int averageWidth, int idx){
    int fontHalf = (int)rectangle2D.getWidth()/2;
    int centerX = averageWidth/2 ;
    return  averageWidth*idx + centerX + CaptchaUtil.randomOffset(centerX/2) - fontHalf;
  }
}
